package com.dianrui.fmi.activity;

import android.app.ProgressDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */

public class PaymentPasswordActivity extends BaseActivity {
    boolean isHide = false;
    private ProgressDialog mProgressDialog;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.current_pwd_edit)
    EditText currentPwdEdit;
    @BindView(R.id.current_pwd_eyes)
    ImageView currentPwdEyes;
    @BindView(R.id.new_pwd_edit)
    EditText newPwdEdit;
    @BindView(R.id.new_pwd_eyes)
    ImageView newPwdEyes;
    @BindView(R.id.confirm_pwd_edit)
    EditText confirmPwdEdit;
    @BindView(R.id.confirm_pwd_eyes)
    ImageView confirmPwdEyes;

    @Override
    public int getContentViewId() {
        return R.layout.activity_payment_password;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.password_payment));
        mProgressDialog = new ProgressDialog(PaymentPasswordActivity.this);
        currentPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    currentPwdEdit.setTransformationMethod(method);
                } else {
                    isHide = true;
                    currentPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    currentPwdEdit.setTransformationMethod(method);
                }
            }
        });
        newPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    newPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    newPwdEdit.setTransformationMethod(method);
                } else {
                    isHide = true;
                    newPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    newPwdEdit.setTransformationMethod(method);
                }
            }
        });

        confirmPwdEyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                    confirmPwdEyes.setImageResource(R.mipmap.logo_eyes_open);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    confirmPwdEdit.setTransformationMethod(method);
                } else {
                    isHide = true;
                    confirmPwdEyes.setImageResource(R.mipmap.logo_eyes_close);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    confirmPwdEdit.setTransformationMethod(method);
                }
            }
        });


    }


    private void UpdateLoginPwd() {
        mProgressDialog.setMessage(getString(R.string.submiting));
        mProgressDialog.show();
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("old_password", currentPwdEdit.getText().toString().trim());
        jsonObject.addProperty("password", newPwdEdit.getText().toString().trim());
        OkHttpUtils.getInstance().postWithMemberToken(Url.SETINFO, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                finish();
                            } else {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    mProgressDialog.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }


    @OnClick({R.id.back, R.id.saves})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.saves:
                String oldpwd = currentPwdEdit.getText().toString();//旧密码
                String psw1 = newPwdEdit.getText().toString().trim();//新密码
                String psw2 = confirmPwdEdit.getText().toString().trim();//确认新密码

                if (StringUtils.isEmpty(oldpwd)) {
                    ToastUtil.showToast(getString(R.string.please_input_current_password));
                    return;
                }
                if (StringUtils.isEmpty(psw1)) {
                    ToastUtil.showToast(getString(R.string.please_input_new_password));
                    return;
                }
                if (psw1.length() < 6) {
                    ToastUtil.showToast(getString(R.string.password_need_six_characters));
                    return;
                }
                if (psw2.length() < 6) {
                    ToastUtil.showToast(getString(R.string.password_need_six_characters));
                    return;
                }
                if (StringUtils.isEmpty(psw2)) {
                    ToastUtil.showToast(getString(R.string.please_input_current_password_again));
                    return;
                }

                if (!psw1.equals(psw2)) {
                    ToastUtil.showToast(getString(R.string.reconfirm));
                    return;
                }
                UpdateLoginPwd();
                break;

                default:
                    break;
        }
    }
}
